package com.longrise.android.byjk.plugins.poster;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.AccessCount;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.ImageUtil;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BasePresenter;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;


/**
 * Created by godliness on 2017/9/25.
 */

public class PosterListPresenter extends BasePresenter<PosterListView> {

    private static final String TAG = "PosterListPresenter";
    private String url, cardNum;

    @Override
    protected void init() {
    }

    public void getTabData() {
        EntityBean bean = new EntityBean();
        bean.set("malltype", "1003");
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_vip_sPostersGoodstype", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        EntityBean[] resultbean = bean.getBeans("result");
                        mView.showData(resultbean);
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }

    public void updateReadStatus(EntityBean bean) {
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        PrintLog.e("bean", " value = " + bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_vip_uPostersInfoByViewnum", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if(value == 1){
                        AccessCount.getInstance().removeReadAll();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }

    public void updateShareStatus(EntityBean bean) {
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        PrintLog.e("data", " data =" + bean.toString());
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl,
                "bbt_vip_uPostersInfoBySharenum", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if(value == 1){
                        AccessCount.getInstance().removeShareAll();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }

//    public void getFeedBackUrl() {
//        EntityBean bean = new EntityBean();
//        bean.set("devicetype", "android");
//        bean.set("urltype", "fkyj");
//        EntityBean fbean = new EntityBean();
//        fbean.set("bean", bean);
//        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_getAppEntryPath", fbean, new LoadDataManager.OnRequestCallListener2() {
//
//            @Override
//            public void onSuccess(String key, String service, Object result) {
//                try {
//                    EntityBean bean = (EntityBean) result;
//                    String resultdesc = bean.getString("resultdesc");
//                    int value = (Integer) bean.get("resultstate");
//                    if(value == 1){
//                        EntityBean resultbean = bean.getBean("result");
//                        mView.setFeedBackUrl(resultbean.getString("pageurl"));
//                    }else {
//                        mView.showToast(resultdesc);
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
//
//            }
//
//            @Override
//            public void onFinished(String key, String service) {
//            }
//
//        });
//
//    }

    public void request() {
        EntityBean bean = new EntityBean();
        bean.set("devicetype", "android");
        bean.set("urltype", "grfx");
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_getAppEntryPath", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        EntityBean resultbean = bean.getBean("result");
                        parse(resultbean);
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }


        });

    }

    private void parse(EntityBean bean) {
        url = bean.getString("pageurl");
        getCardNumUrl();
    }

    private void getUrl() {
        int widthDP = 0;
        StringBuilder builder = new StringBuilder();
        builder.append(url);
        builder.append("?cardno=");
        builder.append(cardNum);
        builder.append("&");
        builder.append("userid=");
        builder.append(UserInfor.getInstance().getUserid());
        builder.append("&");
        builder.append("screenwidth=");
        try {
            int screenWidth = AppUtil.getScreenWidth();
            widthDP = AppUtil.px2dip(screenWidth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        builder.append(widthDP);
        url = builder.toString();
//        UserInfor.getInstance().setUrl(url);
        getShortUrl();
    }

    private void getShortUrl() {
        EntityBean bean = new EntityBean();
        bean.set("url", url);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_user_ParseoShortUrl", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        url = bean.getString("result");
                        UserInfor.getInstance().setUrl(url);
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });
    }

    private void getCardNumUrl() {
        EntityBean bean = new EntityBean();
        bean.set("data", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_common_desData", fbean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = bean.getString("resultdesc");
                    int value = (Integer) bean.get("resultstate");
                    if (value == 1) {
                        cardNum = bean.getString("result");
                        getUrl();
                    } else {
                        mView.showToast(resultdesc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

            @Override
            public void onFinished(String key, String service) {
            }

        });

    }

    public void getUserIconBitmap(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(!AppUtil.isEmpty(UserInfor.getInstance().getUserHeadIcon())){
                    ImageUtil.returnBitMap(UserInfor.getInstance().getUserHeadIcon());
                }
            }
        }).start();

//        if(!StringUtils.isEmpty(UserInfor.getInstance().getUserHeadIcon())){
//            Glide.with(mContext).load(UserInfor.getInstance().getUserHeadIcon()).asBitmap().into(new SimpleTarget<Bitmap>(250, 250) {
//                @Override
//                public void onResourceReady(Bitmap bmp, GlideAnimation glideAnimation) {
//                    UserInfor.getInstance().setBitmap(bmp);
//                }
//            });
//        }
    }
}
